package com.coder4j.starter.mybatis.provider;

import com.coder4j.starter.tools.util.Console;

/**
 * 基础的删除语句提供者
 *
 * @author Keller
 * @date 2019-07-18 09:56:43
 */
public class BaseDeleteProvider {

    /**
     * 根据ID删除
     *
     * @param providerVO table 表名
     *                   id ID
     * @return SQL语句
     */
    public static String deleteById(ProviderVO providerVO) {
        String sql = getDeletePrefix(providerVO.getTableName()) + " WHERE id = #{id} ";
        Console.info("deleteById", providerVO.getTableName(), sql, providerVO.getId());
        return sql;
    }

    /**
     * 根据ID列表删除
     *
     * @param providerVO tableName 表名
     *                   idList id列表
     * @return SQL语句
     */
    public static String deleteByIds(ProviderVO providerVO) {
        String sql = "<script>"
                + getDeletePrefix(providerVO.getTableName())
                + " WHERE id IN "
                + "<foreach collection='idList' item='item' index='index' open='(' separator=',' close=')'>"
                + " #{item}"
                + "</foreach>"
                + "</script>";
        Console.info("deleteByIds", providerVO.getTableName(), sql, providerVO.getIdList());

        return sql;
    }

//
//    public static <T, K extends BaseQuery<T>> String deleteByQuery(K query) {
//
//        String tableName = query.getTableName();
//        String sql = "<script>"
//                + getDeletePrefix(tableName)
//                + SqlFieldReader.getDynamicTableCondition(query)
//                + "</script>";
//        Console.info("deleteByQuery", tableName, sql, JSON.toJSONString(query));
//        return sql;
//    }

    /**
     * 基础的删除语句 delete from tableName
     *
     * @param taleName 表名
     * @return SQL语句
     */
    private static String getDeletePrefix(String taleName) {
        return "DELETE FROM " + taleName + " ";
    }
}
